package com.nop.ui.client.datasources;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;

public class MdpUsersDataSource extends RestDataSource
{
	
	//	public static String					SRV_BASE_URL	= UrlPath.getWebAppRootPath() + "rest/protect";
	//	String urlPath = Window.Location.getPath();
	//	int webappPath = urlPath.lastIndexOf( '/' );
	//	if ( webappPath > 0 )
	//		return urlPath.substring( 0, webappPath + 1 );
	//	else return urlPath;
	//		String urlPath = Window.Location.getPath(); //GWT.getHostPageBaseURL(); //  Window.Location.getPath();
	//		int mainHtmlPresentPos = urlPath.indexOf( MAIN_HTML_NAME );
	//		if ( mainHtmlPresentPos > 0 )
	//			return urlPath.substring( 0, mainHtmlPresentPos );
	//		else return urlPath;
	
	public static String					URL	= GWT.getHostPageBaseURL() + "rest/mdp/mdpusers";
	
	private final RestDataSource	dataSource;
	
	public MdpUsersDataSource()
	{
		this.dataSource = new RestDataSource();
		
		DataSourceTextField id = new DataSourceTextField( "id", "id" );
		id.setRequired( true );
		id.setPrimaryKey( true );
		id.setHidden( true );
		
		DataSourceTextField login = new DataSourceTextField( "login", "login" );
		login.setRequired( true );
		
		DataSourceTextField pass = new DataSourceTextField( "pass", "pass" );
		pass.setRequired( true );
		
		OperationBinding fetch = new OperationBinding();
		fetch.setOperationType( DSOperationType.FETCH );
		fetch.setDataProtocol( DSProtocol.GETPARAMS );
		this.dataSource.setFetchDataURL( MdpUsersDataSource.URL );
		
		OperationBinding add = new OperationBinding();
		add.setOperationType( DSOperationType.ADD );
		this.dataSource.setAddDataURL( MdpUsersDataSource.URL );
		add.setDataProtocol( DSProtocol.GETPARAMS );
		
		OperationBinding remove = new OperationBinding();
		remove.setOperationType( DSOperationType.REMOVE );
		this.dataSource.setRemoveDataURL( MdpUsersDataSource.URL );
		remove.setDataProtocol( DSProtocol.GETPARAMS );
		
		OperationBinding update = new OperationBinding();
		update.setOperationType( DSOperationType.UPDATE );
		this.dataSource.setUpdateDataURL( MdpUsersDataSource.URL );
		update.setDataProtocol( DSProtocol.GETPARAMS );
		
		this.dataSource.setOperationBindings( fetch, add, remove, update );
		this.dataSource.setFields( id, login, pass );
		this.dataSource.setXmlRecordXPath( "MdpUsersInfo" );
	}
	
	public RestDataSource getDataSource()
	{
		return this.dataSource;
	}
	
}